package io.lxw.test.controller;

import com.detabes.search.es.service.EsIndexService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * 检索
 *
 * @author 154594742@qq.com
 * @date 2021/3/5 10:02
 */

@RestController
@RequestMapping("mapping")
public class MappingController {

	private final EsIndexService esIndexService;

	public MappingController(EsIndexService esIndexService) {
		this.esIndexService = esIndexService;
	}


	/**
	 * 新增索引映射
	 *
	 * @param index  索引
	 * @param fields 映射字段
	 * @return ResponseVo
	 */
	@PostMapping("createIndexMapping")
	public boolean createIndexMapping(String index, String fields) throws Exception {
		List<String> str1 = Arrays.asList(fields.split(","));
		return esIndexService.createIndexMapping(index, str1);
	}

	/**
	 * 新增索引
	 *
	 * @param index 索引
	 * @return ResponseVo
	 */
	@PostMapping("createIndex")
	public boolean createIndex(String index) throws IOException {
		return esIndexService.createIndex(index);
	}

	/**
	 * 单个请求，dataId已经有了，需要其它字段
	 *
	 * @param index        索引名称
	 * @param nested       嵌套名称
	 * @param nestedFields 嵌套字段
	 * @return boolean
	 * @author lxw
	 * @date 2021/6/21 15:38
	 */
	@PostMapping("createNested")
	public boolean createNested(String index, String nested, String nestedFields) throws Exception {
		List<String> str2 = Arrays.asList(nestedFields.split(","));
		return esIndexService.createIndexMapping(index, nested, str2);
	}

	/**
	 * 索引是否存在
	 *
	 * @param index index
	 * @return ResponseVo
	 */
	@GetMapping("index/{index}")
	public boolean existIndex(@PathVariable String index) throws IOException {
		return esIndexService.isIndexExist(index);
	}

	/**
	 * 删除索引
	 *
	 * @param index index
	 * @return ResponseVo
	 */
	@PostMapping("deleteIndex")
	public boolean deleteIndex(String index) throws IOException {
		return esIndexService.deleteIndex(index);
	}

	/**
	 * 新增/更新数据
	 *
	 * @param index 索引
	 * @param esId  esId
	 * @return ResponseVo
	 */
	@PostMapping("submitData")
	public boolean submitData(String index, String esId) throws IOException {
		Map<String, Object> source = new HashMap<>();
		source.put("esOnlyId", esId);
		source.put("id", 2);
		source.put("classNo", "2021002");
		source.put("className", "2021级一班");
		source.put("mainInstructorNo", "t00000002");
		source.put("mainInstructorName", "2号教师");
		source.put("createTime", "2021-06-09 17:25:42");
		source.put("createUserName", "admin");
		source.put("updateTime", "2021-06-09 17:25:42");
		source.put("updateUserName", "admin");
		source.put("status", 1);
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> source1 = new HashMap<>();
		source1.put("instructorNo", "t00000002");
		source1.put("instructorName", "2号教师");
		list.add(source1);
		source1 = new HashMap<>();
		source1.put("instructorNo", "t00000003");
		source1.put("instructorName", "3号教师");
		list.add(source1);
		source.put("instructors", list);
		return esIndexService.submitData(index, source, esId);
	}

	/**
	 * 通过ID 更新数据,保证实时性
	 *
	 * @param source 更新的数据
	 * @param index  索引，类似数据库
	 * @param esId   索引数据ID
	 * @return string
	 */
	@PostMapping("updateDataByIdNoRealTime")
	public boolean updateDataByIdNoRealTime(Map<String, Object> source, String index, String esId) throws IOException {

		return esIndexService.updateDataByIdNoRealTime(index, source, esId);
	}

	/**
	 * 通过id删除数据
	 *
	 * @param index index
	 * @param id    id
	 * @return ResponseVo
	 */
	@PostMapping("deleteDataById")
	public boolean deleteDataById(String index, String id) throws IOException {
		return esIndexService.deleteDataById(index, id);
	}

}
